<template>
  <div>
    <van-search
      v-model="params.billNo"
      placeholder="请输入供货者名称、进货单号"
      @search="onSearch"
      @clear="clearSearch"
    />
    <!-- <template v-if="false"> -->
    <div class="page_space">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <base-card
          v-for="(item, index) in list"
          :key="index"
          :data="item.data"
          :title="item.title"
          class="mt-15"
          @click="hitThis(item)"
        />
      </van-list>
    </div>
    <!-- <div class="btn_wrap">
      <button class="btn large">添加</button>
    </div> -->
    <!-- </template> -->
    <!-- <div v-else class="empty_tip">
      <img src="@/assets/img/empty.png" alt="" />
      <p>无数据</p>
      <button class="btn small mt-15" @click="goto('/new-purchaseList')">立即添加</button>
    </div> -->
  </div>
</template>

<script>
import baseCard from '@/components/base-card'
import { getPurchaseList } from '@/api/subject'
import { store, mutations } from '@/observable'

export default {
  name: 'selectPurchaseList',
  components: { baseCard },
  data() {
    return {
      loading: false,
      finished: false,
      params: {
        // limit: 10,
        // page: 1,
        intercourseEnterpriseId: '', // 经营主体 id 2310111033106970000
        varietyId: '', // 样品 varietyId 1730
        billNo: ''
      },
      list: []
    }
  },
  created() {
    this.params.intercourseEnterpriseId =
      this.$route.query.intercourseEnterpriseId
    this.params.varietyId = this.$route.query.varietyId
  },
  methods: {
    onSearch() {
      this.getList(this.params, true)
    },
    clearSearch() {
      this.params.billNo = ''
      this.getList(this.params, true)
    },
    // 触底加载
    onLoad() {
      this.getList(this.params)
    },
    // 选中
    hitThis(e) {
      mutations.setPurchaseInfo(e)
      this.$router.go(-1)
    },
    async getList(params, isReset) {
      if (isReset) {
        this.loading = true
        this.finished = false
        this.list = []
        // this.params.page = 1
      }

      const res = await getPurchaseList(params)
      if (res.code === '0') {
        res.data.forEach((item) => {
          this.list.push({
            title: item.intercourseEnterpriseName,
            relationBillDetailId: item.detailId,
            billNo: item.billNo,
            secondNum: item.secondNum, // 进货数量
            intercourseEnterpriseName: item.intercourseEnterpriseName, // 供货者名称
            produceDate: item.billDate, // 进货日期
            field10: item.contactsTel, // 经货者联系方式
            areaName: item.allAreaName, // 产地
            originPlaceInfoName: item.originPlaceInfoName,
            importFlag: JSON.parse(item.importFlag),
            producer: item.importFlag === 'false' ? 'FALSE' : 'TRUE',
            producerName: item.importFlag === 'false' ? '国产' : '进口',
            data: [
              {
                key: '进货品种',
                value: item.businessVarietyName
              },
              {
                key: '进货单号',
                value: item.billNo
              },
              {
                key: '进货数量',
                value: item.secondNum
              },
              {
                key: '产地',
                value: item.allAreaName
              }
            ]
          })
        })
        // this.params.page++
        // 加载状态结束
        this.loading = false
        // 数据全部加载完成
        // if (res.data.length === 0) {
        this.finished = true
        // }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.empty_tip {
  text-align: center;
  margin-top: 50px;

  font-family: PingFang SC Medium;
  font-size: 14px;
  font-weight: normal;
  line-height: 19px;
  letter-spacing: 0px;
  color: #b2b8bd;
}
</style>
